/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-30 15:56:24
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-08-06 14:40:07
 * @FilePath: \A05\Drive\battery.h
 * @Description: 电池管理头文件
 */
#ifndef __BATTERY_H_
#define __BATTERY_H_


#include "Base.h"
#include "key.h"
#include "kalman.h"


/*增量式pid*/
#define KP              _Q15(0.5)
#define KI              _Q15(0.01)
#define KD              _Q15(0.0)
#define OUT_max         (1)           //EPWM_DUTY_1000(5)         
#define OUT_min         (-1)           //EPWM_DUTY_1000(1)    


#define KP_Vout              _Q15(0.5)
#define KI_Vout              _Q15(0.1)
#define KD_Vout              _Q15(0.01)
#define OUT_max_Vout         (5)                  
#define OUT_min_Vout         (-10)               

#define DATA_NUM        20


#define Pre_Charge_timeout_max      (300)           /*预充电超时判断时间 单位:s*/
#define Charge_off_time             (1200)          /*定时关断时间 单位:minutes*/

#define OpenSample_Con_time         (2000)          /*开路电压检测 充电开时间:1ms*/
#define OpenSample_Coff_time        (100)            /*充电关时间:1ms*/
#define OpenSample_period           (OpenSample_Con_time + OpenSample_Coff_time) /*总时间*/

#define Pre_Charge_Voltage          (2900)          /*预充电完成电压 单位:mv*/
#define Pre_Charge_hys_vol          (2800)          /*预充电滞回电压 单位:mv*/
#define Pre_Charge_end_time         (200)           /*预充电结束判断时间 单位:ms*/

#define Vout_up_limt                (4250)          /*充电输出电压上限 单位:mv*/
#define Vout_low_limt               (4200)          /*充电输出电压下限 单位:mv*/
#define Vout_fault_time             (50)           /*充电输出电压故障计数 单位:10ms*/

#define OC_05A_MAX                  (750)           /*0.5A过流检测*/
#define OC_10A_MAX                  (1500)          /*1A过流检测*/
#define OC_15A_MAX                  (2250)          /*1.5A过流检测*/
#define OC_20A_MAX                  (3000)          /*2A过流检测*/
#define Over_Current_time           (100)          /*过流计数阈值 单位:10ms*/

#define Short_Current_MA            (5000)          /*短路保护电流 单位:mA*/
#define Short_Current_hys_MA        (4000)          /*短路迟滞*/
#define Short_Current_time          (2)             /*短路保护时间阈值 单位:ms*/


#define Reverse_connect_time        (20)           /*反接保护时间阈值 单位:ms*/

#define Low_Power_voltage           (2000)          /*低压/无法激活电池 单位:mv*/
#define Low_Power_hys_voltage       (2200)          /*迟滞*/
#define Low_Power_time              (1000)          /*低压/无法激活电池保护时间阈值 单位:ms*/


/* ******************* 电池插入检测 ******************* */
#define BATTERY_INSERT_VOLTAGE      (4400)           /*电池插入电压 单位:mv*/
#define BATTERY_PULLOUT_VOLTAGE     (4800)           /*电池拔出电压 单位:mv*/
#define BATTERY_INSERT_DETECT_TIME  (150)           /*电池插入检测时间 单位:10ms*/
#define BATTERY_PULLOUT_DET_TIME    (10)           /*拔出检测时间 单位:10ms*/


/*电压电量判断 单位:mv*/
// #define BAT_STAGE_ONE_VOL           (2500)
// #define BAT_STAGE_ONE_LAG_VOL       (2400)

// #define BAT_STAGE_TWO_VOL           (3000)
// #define BAT_STAGE_TWO_LAG_VOL       (2900)


// #define BAT_STAGE_THREE_VOL         (3600)
// #define BAT_STAGE_THREE_LAG_VOL     (3500)


// #define BAT_STAGE_FOUR_VOL          (4150)
// #define BAT_STAGE_FOUR_LAG_VOL      (4050)


#define BAT_STAGE_ONE_VOL           (3550)
#define BAT_STAGE_ONE_LAG_VOL       (3500)

#define BAT_STAGE_TWO_VOL           (3800)
#define BAT_STAGE_TWO_LAG_VOL       (3750)


#define BAT_STAGE_THREE_VOL         (4000)
#define BAT_STAGE_THREE_LAG_VOL     (3950)


#define BAT_STAGE_FOUR_VOL          (4200)      //4150
#define BAT_STAGE_FOUR_LAG_VOL      (4150)

#define BAT_STAGE_JUDG_TIME         (100)       /*单位:10ms*/

/*电池长度检测*/
#define BATTERY_LENTH_DET_TIME      (200)           /*电池长度检测时间:ms*/


typedef struct{
    uint16_t data;
    uint16_t data_arr[DATA_NUM];
    uint16_t avg_data;
}Adc_t;



typedef enum{
    BAT_IDLE         ,
    BAT_WAIT_ACTIVAT ,
    BAT_PreChage     ,
    BAT_Charge       ,
    BAT_Charge_off   ,
    BAT_FULL         ,
    BAT_Fault
}Bat_state_t;


typedef union 
{
    uint16_t Byte;
    struct{
        uint16_t Low_power      : 1;        /*低压/无法激活带保护板的电池*/
        uint16_t Over_voltage   : 1;        /*充电输出过压*/
        uint16_t Short_circuit  : 1;        /*短路*/
        uint16_t Reverse_conect : 1;        /*反接*/
        uint16_t OverCurrent    : 1;        /*过流*/
        uint16_t PreC_TimeOut   : 1;        /*预充超时*/
        uint16_t Temp_Fault     : 1;        /*温度故障*/
    }Bit;
}Error_t;


typedef struct{
    uint32_t Kp       ;
    uint32_t Ki       ;
    uint32_t Kd       ;
    int32_t err1     ;         /*上一次误差*/
    int32_t err2     ;         /*上2次误差*/
    int32_t out_max  ;
    int32_t out_min  ;
}pid_t;

typedef enum{
    CURRENT_05A ,
    CURRENT_10A,
    CURRENT_15A,
    CURRENT_20A
}current_stage_t;


typedef enum{
    BAT_STAGE_ZERO = 0 ,
    BAT_STAGE_ONE   ,
    BAT_STAGE_TWO   ,
    BAT_STAGE_THREE ,
    BAT_STAGE_FOUR
}stage_t;

typedef struct{
    uint16_t sure_cnt  ;
    uint16_t cancel_cnt;
    stage_t  stage     ;
}Voltage_stage_t;



typedef void (*base_func_t)(void);

typedef struct{
    Bat_state_t state;          /*电池状态*/
    Bat_state_t pre_state;      /*进入故障态之前的状态*/

    Adc_t adc_voltage;          /*电池电压采样*/
    Adc_t adc_current;          /*电池电流采样*/
    Adc_t adc_vout;             /*充电输出电压*/

    KFPTypeS kfp_current;       /*卡尔曼滤波电流*/

    uint16_t current_offset;    /*电流偏置ADC值*/
    float current_gain;         /*电流放大倍数 :1.0x*/
    uint16_t current_last_filter;   /*电流上次限幅滤波值*/
    uint16_t curlimtFilter_data;   /*电流限幅滤波值*/

    key_t key;                  /*调电流按键*/

    uint32_t realtime_voltage;  /*实时电压*/
    uint32_t voltage;           /*电池真实电压 单位:mv*/
    uint32_t current;           /*电池真实电流 单位:ma*/
    uint32_t capacity;          /*电池充入容量*/
    uint32_t charge_time;       /*充电时间 单位:min*/
    uint8_t charge_off_flag;    /*定时关断标志*/

    uint32_t vout_voltage;      /*充电输出电压 单位:mv*/

    Voltage_stage_t vol_stage;  /*电压阶段*/

    uint8_t vol_samp_flag;      /*电压开始采样标志*/

    uint8_t preCharge_end;      /*预充电结束标志*/
    uint16_t preCharge_end_cnt; /*预充电结束计数*/

    uint16_t vol_full_cnt;          /*满电cnt*/
    uint16_t current_full_cnt;      /*电流满电*/

    uint32_t pre_charg_time;    /*预充电时间 单位:s*/
    uint16_t lv_cnt;            /*低压计数*/
    uint16_t ov_cnt;            /*过压计数*/
    uint16_t short_cnt;         /*短路计数*/
    uint16_t reverse_cnt;       /*反接计数*/
    uint16_t oc_cnt;            /*过流计数*/
    Error_t err;                /*故障*/
    uint16_t short_prot_value;  /*短路保护值*/

    uint8_t temp_pro_recover;   /*温度保护恢复标志*/

    uint16_t insert_cnt;        /*插入计数*/
    uint16_t pull_out_cnt;      /*拔出计数*/
    uint8_t insert_flag;        /*电池插入标志*/
    uint8_t reve_prot_pull_out; /*反接保护拔出标志*/

    uint8_t active_flag;        /*激活标志*/
    uint16_t active_finish_cnt; /*激活完成计数*/

    uint16_t over_current_value;/*过流保护值*/
    uint8_t current_Modify;     /*电流设定已改变*/
    current_stage_t current_stage;  /*电流档位*/
    uint8_t doubleBat;          /*双电池*/
    current_stage_t doubleBatPreStage;   /*上次电流档位*/

    enum{
        NORMAL_CHARGE = 0 ,         /*正常充电*/
        SET_CURRENT ,               /*设置电流*/
        SET_CURRENT_END             /*设置电流结束*/
    }current_display_state;
    uint16_t flash_time_cnt;    /*闪烁计数*/

    uint16_t flash_time;        /*闪烁时间*/

    uint16_t err_flash_time_cnt; /*故障闪烁计数*/
    uint8_t err_show_on;        /*故障显示开关*/

    uint8_t page_SetOrEnd;      /*电流设置开始or结束*/
    uint8_t page_modif_curr;    /*修改电流设定标志*/

    uint16_t current_set;       /*电流设定值:mA*/
    pid_t pid;                  /*pid调节电流*/
    uint32_t pwm_ccr;           /*pwm duty调节值*/
    uint8_t pid_start;          /*pid开始调节*/
    uint8_t pid_delay;          /*延时开启调节pwm*/
    uint8_t dis_chg_delay;      /*延时关闭enable*/

    pid_t volPid;               /*恒压*/
    uint16_t volCtlCCR;        /*电压控制pwm占空比*/

    uint16_t modeChagCnt;       /*模式切换计数*/
    uint16_t constVolCancelCnt;
    enum{
        ConstCurrent = 0,      /*正常充电*/
        ConstVoltage,          /*恒压充电*/
    }charge_mode;
    
    uint8_t screen_refresh_time;/*屏幕刷新时间*/
    uint8_t breath_time;        /*呼吸时间*/
    uint8_t breath_stage;       /*电量呼吸*/
    uint8_t fast_show;          /*快速显示*/

    base_func_t dis_charge;     /*关闭充电输出*/
    base_func_t en_charge;      /*打开充电输出*/

    void (*regula_pwm)(uint32_t duty);  /*调节占空比*/
    void (*pwm_off)(void);              /*关闭pwm*/
    void (*pwm_on)(void);               /*打开pwm*/
    
}Battery_t;



void current_set_process(Battery_t * pBat);

void Battery_state_machine(Battery_t * pBat);


void pwm_off_L(void);
void pwm_on_L(void);

void pwm_off_R(void);
void pwm_on_R(void);

void dis_charge_L(void);

void en_charge_L(void);

void dis_charge_R(void);

void en_charge_R(void);

uint8_t lenth_det_L(void);

uint8_t lenth_det_R(void);

void regula_pwm_L(uint32_t duty);

void regula_pwm_R(uint32_t duty);

void Timed_off_charge(Battery_t * pBat);

void PreCharge_end_detect(Battery_t *pBat);

void Active_detect(Battery_t * pBat);

void Charge_current_set(Battery_t * pBat);

void Battery_insert_detect(Battery_t *pBat);

void Capacity_calculate(Battery_t * pBat);

void Bat_Satge_judg(Battery_t * pBat);

//void Battery_lenth_detect(Battery_t *pBat);

/******** 故障检测 ********/
void PreCharge_timeout_det(Battery_t * pBat);

void Vout_over_detect(Battery_t * pBat);

void Short_circuit_detect(Battery_t * pBat);

void Over_current_detect(Battery_t * pBat);

void Reverse_connect_detect(Battery_t * pBat);

void Low_voltage_detect(Battery_t * pBat);

void clear_fault_cnt(Battery_t * pBat);

int32_t Pid_calculate(pid_t * pPid , uint32_t target , uint32_t current);

uint16_t Filter_calculate(uint16_t * pData , uint32_t data , uint8_t data_num);

int32_t Temp_Pid_calculate(pid_t * pPid , uint32_t target , uint32_t current);

uint16_t LimitFilter(uint16_t newdata , uint16_t* lastLimtValue , uint16_t limit);

#endif


